package com.jiegui.modules.wechat.controller;

import com.jiegui.common.core.redis.RedisCache;
import com.jiegui.common.utils.XmlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;

/**
 * @description: 回调
 * @author: wuqicheng
 * @time: 15/12/2021 1:41 PM
 */
@RestController
@Slf4j
@RequestMapping("/wx/notify/")
public class NotifyController {

    @Resource
    RedisCache redisCache;


    /**
     * 用户充值通知接口
     */
    @RequestMapping(value = "/rechargeNotify")
    public void rechargeNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("*************** 微信支付通知(用户充值)START ****************");

        try {
            Map<String, String> resMap = XmlUtils.wxparseXml(request);
            log.info("微信支付通知原文: [{}]", resMap);

            String tradeNo = redisCache.getCacheObject(resMap.get("out_trade_no") + "-recharge");
            if (StringUtils.isEmpty(tradeNo)) {
                redisCache.setCacheObject(resMap.get("out_trade_no") + "recharge", tradeNo,7, TimeUnit.DAYS);
               // notifyService.rechargePayNotify(PaymentHelper.wxnotifyConver(resMap));
            }

            SortedMap<String, String> returnMap = new TreeMap<>();
            returnMap.put("return_code", "SUCCESS");
            response.getWriter().write(XmlUtils.toXml(returnMap));
        } catch (Exception e) {
            log.error("微信支付通知处理异常: ", e);
            try {
                SortedMap<String, String> returnMap = new TreeMap<>();
                returnMap.put("return_code", "FAIL");
                response.getWriter().write(XmlUtils.toXml(returnMap));
            } catch (Exception ex) {
                log.error("微信支付响应微信端异常：", ex);
            }
        }
    }


    /**
     * 开柜充电回调
     */
    @RequestMapping(value = "/chargeNotify")
    public void chargeNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("*************** 微信支付通知(开柜充电)START ****************");

        try {
            Map<String, String> resMap = XmlUtils.wxparseXml(request);
            log.info("微信支付通知原文: [{}]", resMap);

            String tradeNo = redisCache.getCacheObject(resMap.get("out_trade_no") + "charge");
            if (StringUtils.isEmpty(tradeNo)) {
                redisCache.setCacheObject(resMap.get("out_trade_no") + "charge", tradeNo,1, TimeUnit.DAYS);
                //notifyService.chargePayNotify(PaymentHelper.wxnotifyConver(resMap));
            }

            SortedMap<String, String> returnMap = new TreeMap<>();
            returnMap.put("return_code", "SUCCESS");
            response.getWriter().write(XmlUtils.toXml(returnMap));
        } catch(Exception e) {
            log.error("微信支付通知处理异常: ", e);
            try {
                SortedMap<String, String> returnMap = new TreeMap<>();
                returnMap.put("return_code", "FAIL");
                response.getWriter().write(XmlUtils.toXml(returnMap));
            } catch (Exception ex) {
                log.error("微信支付响应微信端异常：", ex);
            }
        }
    }



    /**
     * 充电占箱支付回调
     */
    @RequestMapping(value = "/chargeTimeOutNotify")
    public void chargeTimeOutNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("*************** 微信支付通知(占箱超时支付)START ****************");

        try {
            Map<String, String> resMap = XmlUtils.wxparseXml(request);
            log.info("微信支付通知原文: [{}]", resMap);

            String tradeNo = redisCache.getCacheObject(resMap.get("out_trade_no") + "timeOut");
            if (StringUtils.isEmpty(tradeNo)) {
                redisCache.setCacheObject(resMap.get("out_trade_no") + "timeOut", tradeNo,1, TimeUnit.DAYS);
               // notifyService.chargeTimeOutNotify(PaymentHelper.wxnotifyConver(resMap));
            }

            SortedMap<String, String> returnMap = new TreeMap<>();
            returnMap.put("return_code", "SUCCESS");
            response.getWriter().write(XmlUtils.toXml(returnMap));
        } catch(Exception e) {
            log.error("微信支付通知处理异常: ", e);
            try {
                SortedMap<String, String> returnMap = new TreeMap<>();
                returnMap.put("return_code", "FAIL");
                response.getWriter().write(XmlUtils.toXml(returnMap));
            } catch (Exception ex) {
                log.error("微信支付响应微信端异常：", ex);
            }
        }
    }


    @RequestMapping("/wechatCore")
    public void wechatGet(HttpServletRequest request,
                          HttpServletResponse response,
                          @RequestParam(value = "signature") String signature,
                          @RequestParam(value = "timestamp") String timestamp,
                          @RequestParam(value = "nonce") String nonce,
                          @RequestParam(value = "" +
                                  "echostr") String echostr) throws IOException {
        System.out.println("进入验证");
                        response.getWriter().print(echostr);
        }

}
